package org.example.websocket.spring.client;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.client.WebSocketConnectionManager;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;

import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;

@Configuration
public class WebSocketClientConfig {

    @Bean
    public WebSocketConnectionManager wsConnectionManager(WebSocketClientHandler handler) {
        WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), handler,
                "ws://localhost:9001/spring/ws?token=abc");
        manager.setAutoStartup(true);
        return manager;
    }

    @Bean
    public StandardWebSocketClient client() {
        //设置最大消息大小和最大超时时间
        WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
        webSocketContainer.setDefaultMaxBinaryMessageBufferSize(8092 * 10);
        webSocketContainer.setDefaultMaxTextMessageBufferSize(8092 * 10);
        webSocketContainer.setDefaultMaxSessionIdleTimeout(15 * 60000L);
        return new StandardWebSocketClient(webSocketContainer);
    }

}
